<?php

class Admin_CategoryController extends ZF_AdminController{
    
	public function indexAction(){
            $model = new Admin_Model_Category();
            $paginator = Zend_Paginator::factory($model->listall());
            $paginator->setItemCountPerPage(20);
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->categories = $paginator;
            $this->view->title = "CATEGORY MANAGEMENT";
            
	}
        public function addAction(){
            $form = new Admin_Form_AddCategoryForm();
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($_POST)) {
                   
                    $model = new Admin_Model_Category();
                    $result = $model->insert(
                                    $form->getValue("name"),
                                    $form->getValue("name_en"));
                                           
                    if ($result>0) {
                        $this->_redirect("/".$this->module."/category/index");
                    }
                }
            } else {
                $form->setAction($this->baseUrl."/".$this->module."/category/add");
                $this->view->form = $form;
                $this->view->title = "ADD CATEGORY";
                $this->view->group = "CATEGORY";
                $this->view->group_action = "ADD";

            }
        }
        public function editAction(){
            $id = $this->_request->getParam('id');
            $form = new Admin_Form_EditCategoryForm();
            $model = new Admin_Model_Category();
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($_POST)) {
                    
                    $model->getById($form->getValue("id"));
                    $model->setName($form->getValue("name"));
                    $model->setName_En($form->getValue("name_en"));
                   
                    $result=$model->update();
                    if ($result>0) 
                    {
                        $this->_redirect("/".$this->module."/category/index");
                    }
                }
            } else {
                $category = $model->getById($id);
                if ($category != null) {
                    $form->populate($category);
                    $form->setAction($this->baseUrl."/".$this->module."/category/edit");
                    $this->view->form = $form;        
                    $this->view->intro = "Update Category";
                    $this->view->group = "CATEGORY";
                    $this->view->group_action = "UPDATE";
                }
                
            }
        }
        public function delAction(){
            $id = $this->getRequest()->getParam('id',null);
            $model = new Admin_Model_Category();
            $model->getById($id);
            $result = $model->delete();
            if($result > 0)
                   $this->_redirect("/".$this->module."/category/index");
            else 
                    $this->view->error = "Không thể xóa !!!";
             $this->view->group = "CATEGORY";
                $this->view->group_action = "DELETE";
        }
        
       
       

 
}
?>
